package yjj.utils.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;

import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

public class ExcelUtil {

    /**
     * 导出Excel并发送到前端下载
     *
     * @param response         HttpServletResponse对象
     * @param fileName         下载时的文件名
     *
     * @param sheetName        工作表名称
     * @param clazz            数据对应的实体类
     * @param data             导出的数据列表
     */
    public static void exportExcel(HttpServletResponse response,
                                   String fileName,

                                   String sheetName,
                                   Class<?> clazz,
                                   List<?> data) {
        try {
            // 设置响应的内容类型为Excel
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            // 处理文件名的编码，防止中文乱码
            String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);

            // 创建表头样式
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); // 设置背景为灰色
            headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 12); // 字体大小
            headWriteCellStyle.setWriteFont(headWriteFont);

            // 创建内容样式
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
            // contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
           // contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); // 内容背景为白色
            WriteFont contentWriteFont = new WriteFont();
            contentWriteFont.setFontHeightInPoints((short) 11); // 字体大小
            contentWriteCellStyle.setWriteFont(contentWriteFont);

            // 定义样式策略
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                    new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);



            // 获取响应的输出流
            ServletOutputStream outputStream = response.getOutputStream();

            // 使用EasyExcel写入数据
            EasyExcel.write(outputStream, clazz)
                    .registerWriteHandler(horizontalCellStyleStrategy)

                    .sheet(sheetName)
                    .doWrite(data);

            // 刷新输出流
            outputStream.flush();
        } catch (Exception e) {
            throw new RuntimeException("Excel导出失败", e);
        }
    }
}
